package com.fdb.efp.nls.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.vo.DutyInfo;
import com.fdb.basic.framework.core.vo.RoleInfo;
import com.fdb.basic.framework.core.vo.UserInfo;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.nls.service.facade.AstSysMessageService;
import com.fdb.efp.nls.service.vo.AstSysMessageVO;
import com.fdb.efp.nls.service.vo.SDutyVO;
import com.fdb.efp.nls.service.vo.SRoleVO;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class AstSysMessageController extends AbstractController  {
	
	@Autowired
	@Qualifier("astSysMessageService")
	private AstSysMessageService astSysMessageService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	/**
	 * 列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/ast/sys/messages", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<AstSysMessageVO>> queryAstSysMessageAll(
			@RequestBody AstSysMessageVO vo) {		
		// 根据用户角色获取用户权限方案
		List<AstSysMessageVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = astSysMessageService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = astSysMessageService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = astSysMessageService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = astSysMessageService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/ast/sys/message/{mesId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<AstSysMessageVO> queryByPk(@PathVariable("mesId") String mesId) {		
		AstSysMessageVO inVo = new AstSysMessageVO();
				inVo.setMesId(mesId);
		AstSysMessageVO outputVo = astSysMessageService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param inAstSysMessageVo
	 * @return
	 */
	@RequestMapping(value="/ast/sys/message", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody AstSysMessageVO inAstSysMessageVo) {		
		int outputVo = astSysMessageService.deleteByPk(inAstSysMessageVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inAstSysMessageVo
	 * @return
	 */
	@RequestMapping(value="/ast/sys/message", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody AstSysMessageVO inAstSysMessageVo) {		
		int outputVo = astSysMessageService.updateByPk(inAstSysMessageVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inAstSysMessageVo
	 * @return
	 */
	@RequestMapping(value="/ast/sys/message", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertAstSysMessage(@RequestBody AstSysMessageVO inAstSysMessageVo) {
		setUserInfoToVO(inAstSysMessageVo);
		int outputVo = astSysMessageService.insertAstSysMessage(inAstSysMessageVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 返回首页显示的不分页公告列表数据
	 * @param inAstSysMessageVo
	 * @return
	 */
	
	@RequestMapping(value="/ast/sys/indexView", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<AstSysMessageVO>> queryIndexView(@RequestBody AstSysMessageVO inAstSysMessageVo) {
		List<AstSysMessageVO> outputVo = null;
		try {
			UserInfo user = this.getUserInfo();
			if (Objects.nonNull(user)) {
				//从 session 里面，获取用户的机构、角色、岗位信息， 而不是从前端传过来
				inAstSysMessageVo.setOrgIds(user.getOrgInfo().getOrgCode()); //机构信息
				List<SRoleVO> roleCodes = new ArrayList<>(); //角色信息
				List<RoleInfo> userRoles = user.getUserRoles();
				if (Objects.nonNull(userRoles) && !userRoles.isEmpty()){
					SRoleVO role;
					for (RoleInfo roleInfo: userRoles) {
						role = new SRoleVO();
						role.setRoleCode(roleInfo.getRoleCode());
						roleCodes.add(role);
					}
				}
				inAstSysMessageVo.setRoleCodes(roleCodes);
				List<SDutyVO> dutyCodes = new ArrayList<>(); //岗位信息
				List<DutyInfo> userDutys =  user.getUserDutys();
				if (Objects.nonNull(userDutys) && !userDutys.isEmpty()){
					SDutyVO duty;
					for (DutyInfo dutyInfo: userDutys) {
						duty = new SDutyVO();
						duty.setDutyCode(dutyInfo.getDutyCode());
						dutyCodes.add(duty);
					}
				}
				inAstSysMessageVo.setDutyCodes(dutyCodes);
				outputVo = astSysMessageService.queryAllCurrOrgView(inAstSysMessageVo);

				if(Objects.nonNull(outputVo) && !outputVo.isEmpty()){
					outputVo = outputVo.stream()
							.sorted(Comparator.comparing(AstSysMessageVO::getSenddate).reversed())
							.collect(Collectors.toList());
				}

			}
		}catch (Exception e){
			logger.error("查询公告列表:", e);
		}
		return getResponseData(outputVo);
	}
	
}
